package edu.guigu.appquickstart.recyclerview.ex1;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.recyclerview.bean.UserBean;

public class UserListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;      // View
    private ArrayList<UserBean> datas;      // Model
    private UserBeanAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        // Actionbar
        setSupportActionBar(findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // 初始化数据
        initData();
        // 初始化视图
        initView();
        // 初始化监听
        initListener();
    }

    private void initData() {
//        datas = new ArrayList<UserBean>();
//        for (int i = 0; i < 50; i++) {
//            final String url = "https://avatars.githubusercontent.com/u/" + (i + 1) + "?v=4";
//            UserBean userBean = new UserBean("张三" + (i + 1), url, "2025-08-08", "2025-09-09");
//            datas.add(userBean);
//        }

        datas = UserBean.loadData(this);
    }

    private void initView() {
        recyclerView = findViewById(R.id.recyclerview);
        adapter = new UserBeanAdapter(this, datas);
        // 注意：设置布局管理器,明确布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        // 可选,设置分隔线
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(itemDecoration);
        // TODO 注册上下文菜单
        registerForContextMenu(recyclerView);
        // 可选
        DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setRemoveDuration(300);
        recyclerView.setItemAnimator(itemAnimator);
    }
    private void initListener() {
        adapter.setOnMyItemViewOnClickListener(new UserBeanAdapter.OnMyItemViewOnClickListener() {
            @Override
            public void onClick(View view,int position) {
                // 获取当前选择的位置
                UserBean userBean = datas.get(position);
                Intent intent = new Intent(UserListActivity.this,ShowActivity.class);
                intent.putExtra("html_url", userBean.getHtml_url());
                startActivity(intent);
            }
        });
    }
    // 上下文菜单----------------------------------------------------------------
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu_recyclerview_ex1_context, menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_detailed) {
            // 获取当前选择的位置
            int position = adapter.getCurrentPosition();
            UserBean userBean = datas.get(position);

            Intent intent = new Intent(this,ShowActivity.class);
            intent.putExtra("html_url", userBean.getHtml_url());
            startActivity(intent);
            return true;
        }
        if (item.getItemId() == R.id.menu_add) {
            int position = adapter.getCurrentPosition();
            final String url = "https://b0.bdstatic.com/ugc/img/2024-12-01/96c0c6ce9dfde51bd7a768c91ecf3082.png";
            UserBean userBean = new UserBean("新加入", url);
            datas.add(position, userBean);
            adapter.notifyItemInserted(position);
            return true;
        }
        if (item.getItemId() == R.id.menu_delete) {
            int position = adapter.getCurrentPosition();
            datas.remove(position);
            adapter.notifyItemRemoved(position);
            return true;
        }
        return super.onContextItemSelected(item);
    }

    // 选项菜单----------------------------------------------------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_recyclerview_ex1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }

        if (item.getItemId() == R.id.menu_list) {
            adapter.setMode(UserBeanAdapter.MODE_LIST);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            return true;
        }

        if (item.getItemId() == R.id.menu_grid) {
            adapter.setMode(UserBeanAdapter.MODE_GRID);
            GridLayoutManager layoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}